<template>
  <div class="app-container ohn">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="用户名称">
        <el-input
          v-model="formInline.name"
          placeholder="请输入证书名称"
          clearable
          @keydown.enter.native="onSearch"
        />
      </el-form-item>
      <el-form-item label="账号状态">
        <el-input
          v-model="formInline.name"
          placeholder="请输入证书名称"
          clearable
          @keydown.enter.native="onSearch"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          v-loading="tableLoading"
          type="primary"
          @click="onSearch"
        >查询</el-button>
        <el-button @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="lb-flex lb-flex-bw">
      <span class="lh32">用户列表</span>
      <el-button
        type="primary"
        plain
        size="small"
        @click="createdDetection"
      >新增</el-button>
    </div>
    <el-table
      v-loading="tableLoading"
      :data="tableData"
      stripe
      border
      style="width: 100%"
      class="mt8"
    >
      <el-table-column prop="userCode" label="登录名" />
      <el-table-column prop="userName" label="昵称" width="70" />
      <el-table-column prop="accountType" label="账号类型" width="120" />
      <el-table-column prop="mobilePhone" label="手机号" />
      <el-table-column prop="email" label="邮箱 " width="180" />
      <el-table-column prop="status" label="账号状态" width="180" />
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleDelCert(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="fr mt20"
      :current-page="pagination.currPage"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pagination.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.pageTotal"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import config from "@/utils/config"
const { prefix } = config[process.env.NODE_ENV]
import { getToken } from "@/utils/auth" // get token from cookie
export default {
  data() {
    return {
      prefix: prefix,
      tableLoading: false,
      formInline: {
        name: "",
        encryptionKey: ""
      },
      tableData: [
        {
          accountType: "员工",
          accountTypeCode: "personnel",
          areaCode: "",
          email: "",
          forceLock: "",
          gradeName: "",
          loginName: "console_super",
          mobilePhone: "",
          objectId: "d4e688ab67cb11e89a3ce9a8d592ce6c",
          score: "",
          status: "ENABLE",
          userCode: "console_super",
          userIcon: "",
          userId: "d4e688ab67cb11e89a3ce9a8d592ce6c",
          userName: "控制台超级管理员",
          workStatus: null,
          workStatusText: ""
        },
        {
          accountType: "员工",
          accountTypeCode: "personnel",
          areaCode: "",
          email: "",
          forceLock: "",
          gradeName: "",
          loginName: "dqmWorkOrder",
          mobilePhone: "",
          objectId: "11ec951cc858ac01bf252bd37d340dcf",
          score: "",
          status: "ENABLE",
          userCode: "dqmWorkOrder",
          userIcon: "",
          userId: "11ec951cc858ac01bf252bd37d340dcf",
          userName: "dqmWorkOrder",
          workStatus: null,
          workStatusText: ""
        },
        {
          accountType: "员工",
          accountTypeCode: "personnel",
          areaCode: "",
          email: "",
          forceLock: "",
          gradeName: "",
          loginName: "gg031022",
          mobilePhone: "",
          objectId: "11eca04b3dcb559c80cee1f66e745c47",
          score: "",
          status: "DISABLE",
          userCode: "12345",
          userIcon: "",
          userId: "11eca04b3dcb559c80cee1f66e745c47",
          userName: "郭郭031022",
          workStatus: null,
          workStatusText: ""
        },
        {
          accountType: "独立账号",
          accountTypeCode: "independentAccount",
          areaCode: "+86",
          email: "",
          forceLock: "",
          gradeName: "",
          loginName: "puyue",
          mobilePhone: "18193866616",
          objectId: "11eca50c7ac40513ac31b756a6d31d7b",
          score: "",
          status: "DISABLE",
          userCode: "yyyyy",
          userIcon: "",
          userId: "11eca50c7ac40513ac31b756a6d31d7b",
          userName: "puyue",
          workStatus: null,
          workStatusText: ""
        },

        {
          accountType: "员工",
          accountTypeCode: "personnel",
          areaCode: "",
          email: "",
          forceLock: "",
          gradeName: "",
          loginName: "gg042102",
          mobilePhone: "",
          objectId: "11ecc1277bf43606b69d759fd1efb699",
          score: "",
          status: "DISABLE",
          userCode: "bbbbb",
          userIcon: "",
          userId: "11ecc1277bf43606b69d759fd1efb699",
          userName: "郭郭042102",
          workStatus: null,
          workStatusText: ""
        },

        {
          accountType: "独立账号",
          accountTypeCode: "independentAccount",
          areaCode: "",
          email: "",
          forceLock: "",
          gradeName: "1564891",
          loginName: "cs",
          mobilePhone: "",
          objectId: "11eccde2fcf46295ac31f9e5aa4a66f9",
          score: "",
          status: "DISABLE",
          userCode: "561564",
          userIcon: "",
          userId: "11eccde2fcf46295ac31f9e5aa4a66f9",
          userName: "测试账号",
          workStatus: null,
          workStatusText: ""
        },
        {
          accountType: "独立账号",
          accountTypeCode: "independentAccount",
          areaCode: "",
          email: "",
          forceLock: "",
          gradeName: "",
          loginName: "cszh",
          mobilePhone: "",
          objectId: "11ecd02783be20ddac31c57cfd3e820c",
          score: "",
          status: "DISABLE",
          userCode: "abc123",
          userIcon: "",
          userId: "11ecd02783be20ddac31c57cfd3e820c",
          userName: "测试账号",
          workStatus: null,
          workStatusText: ""
        },
        {
          accountType: "独立账号",
          accountTypeCode: "independentAccount",
          areaCode: "",
          email: "",
          forceLock: "",
          gradeName: "",
          loginName: "xx",
          mobilePhone: "",
          objectId: "11ecd0d59c1277efac315dbe69e11ae9",
          score: "",
          status: "DISABLE",
          userCode: "4651",
          userIcon: "",
          userId: "11ecd0d59c1277efac315dbe69e11ae9",
          userName: "测试权限",
          workStatus: null,
          workStatusText: ""
        }
      ],
      pagination: {
        currPage: 1,
        pageSize: 10,
        pageTotal: 0
      }
    }
  },
  created() {
    // this.handleSearchCertList();
  },
  methods: {
    handleSearchCertList() {
      this.tableLoading = true
      const fdata = {
        pageNum: this.pagination.currPage,
        pageSize: this.pagination.pageSize,
        name: this.formInline.name,
        encryptionKey: this.formInline.encryptionKey
      }
      this.$store
        .dispatch("cert/getCertList", fdata)
        .then((res) => {
          const { data, success, errorMessage } = res
          if (success) {
            this.tableData = data.list
            this.pagination.currPage = data.pageNum
            this.pagination.pageTotal = data.total
          } else {
            this.$message.error(errorMessage)
          }
        })
        .catch(() => {})
        .finally(() => {
          this.tableLoading = false
        })
    },
    handleDelCert(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$store
            .dispatch("cert/delCert", row.id)
            .then((res) => {
              const { data, success, errorMessage } = res
              if (success) {
                this.$message({
                  message: "删除成功",
                  type: "success"
                })
                this.handleSearchCertList()
              } else {
                this.$message.error(errorMessage)
              }
            })
            .catch(() => {})
            .finally(() => {})
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          })
        })
    },
    handleDownLoadCert(row) {
      this.fileName = row.name
      this.$message({
        type: "info",
        message: "文件正在下载中，请稍后~"
      })
      fetch(
        prefix.lb +
          "/api/certification/downloadByEncryptionKey?encryptionKey=" +
          row.encryptionKey,
        {
          method: "GET",
          responseType: "blob",
          headers: new Headers({
            // 自己加的头信息全都要转成string
            encryptionKey: row.encryptionKey.toString(),
            "ACCESS-TOKEN": getToken().toString()
          })
        }
      )
        .then((res) => res.blob())
        .then((data) => {
          const blobUrl = window.URL.createObjectURL(data)
          const a = document.createElement("a")
          a.download = this.fileName + ".pdf"
          a.href = blobUrl
          a.click()
          this.$message({
            type: "success",
            message: "文件下载成功"
          })
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "文件下载失败，请稍后再试~"
          })
        })
        .finally(() => {})
    },
    onSearch() {
      this.handleSearchCertList()
    },
    resetForm() {
      this.formInline.name = ""
      this.formInline.encryptionKey = ""
      this.handleSearchCertList()
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.handleSearchCertList()
    },
    handleCurrentChange(val) {
      this.pagination.currPage = val
      this.handleSearchCertList()
    }
  }
}
</script>
